package com.mytx.common.utils;

import com.alibaba.fastjson.JSONObject;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Date;

import javax.crypto.SecretKey;

public class HttpEncryptUtil {


    //APP加密请求内容
    public static String appEncrypt( String servicePublicKey,String content) throws Exception{
        //将Base64编码后的Server公钥转换成PublicKey对象
        PublicKey serverPublicKey = RSAUtil.string2PublicKey(servicePublicKey);
        //每次都随机生成AES秘钥
        String aesKeyStr = AESUtil.genKeyAES();
        SecretKey aesKey = AESUtil.loadKeyAES(aesKeyStr);
        //用Server公钥加密AES秘钥
        byte[] encryptAesKey = RSAUtil.publicEncrypt(aesKeyStr.getBytes(), serverPublicKey);
        byte[] encryptRequest = AESUtil.encryptAES(content.getBytes(), aesKey);

        JSONObject result = new JSONObject();
        result.put("ak", RSAUtil.byte2Base64(encryptAesKey));

        result.put("ct", RSAUtil.byte2Base64(encryptRequest));
        result.put("aesKeyStr",aesKeyStr);
        return result.toString();
    }

    //APP解密服务器的响应内容 ,aeskeyStr是客户端请求服务器时候生成的，当客户端响应数据后，用该aesKey解密即可，获得
    public static String appDecrypt(String aesKeyStr, String content) throws Exception{
        JSONObject result = JSONObject.parseObject(content);
        String encryptContent = (String) result.get("ct");

        //用AES秘钥解密请求内容
        SecretKey aesKey = AESUtil.loadKeyAES(aesKeyStr);
        byte[] response = AESUtil.decryptAES(RSAUtil.base642Byte(encryptContent), aesKey);

        return new String(response);
    }

    //服务器加密响应给APP的内容
    public static Object serverEncrypt(String aesKeyStr, Object content) throws Exception{

        //将Base64编码后的AES秘钥转换成SecretKey对象
        SecretKey aesKey = AESUtil.loadKeyAES(aesKeyStr);

        //用AES秘钥加密响应内容
        byte[] encryptContent = AESUtil.encryptAES(JSONObject.toJSONString(content).getBytes(), aesKey);

//        JSONObject result = new JSONObject();
//        result.put("ct", RSAUtil.byte2Base64(encryptContent));
        return RSAUtil.byte2Base64(encryptContent);
    }

    //服务器解密APP的请求内容
    public static JSONObject serverDecrypt(String content,String servicePricateKey) throws Exception{
        JSONObject result = JSONObject.parseObject(content);
        String encryptAesKeyStr = (String) result.get("ak");
        String encryptContent = (String) result.get("ct");

        //将Base64编码后的Server私钥转换成PrivateKey对象
        PrivateKey serverPrivateKey = RSAUtil.string2PrivateKey(servicePricateKey);
        //用Server私钥解密AES秘钥
        byte[] aesKeyBytes = RSAUtil.privateDecrypt(RSAUtil.base642Byte(encryptAesKeyStr), serverPrivateKey);
        //生成AES秘钥
        SecretKey aesKey = AESUtil.loadKeyAES(new String(aesKeyBytes));

        //用AES秘钥解密请求内容
        byte[] request = AESUtil.decryptAES(RSAUtil.base642Byte(encryptContent), aesKey);
        String s = new String(request);
        JSONObject jsonObject = JSONObject.parseObject(s);
        jsonObject.put("ak",new String(aesKeyBytes));

        return jsonObject;
    }

}
